<?php
/**
 * [PHPFOX_HEADER]
 */

defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package  		Module_Blog
 * @version 		$Id: process.class.php 4142 2012-05-02 09:55:48Z Miguel_Espinoza $
 */
class Hoidap_Service_Process extends Phpfox_Service 
{
	/**
	 * Class constructor
	 */	
	public function __construct()
	{
		$this->_sTable = Phpfox::getT('hoidap_cauhoi');
	}	
	
	public function add($aVals)
	{
		// (($sPlugin = Phpfox_Plugin::get('blog.service_process__start')) ? eval($sPlugin) : false);
		$oFilter = Phpfox::getLib('parse.input');		
		
		// check if the user entered a forbidden word
		Phpfox::getService('ban')->checkAutomaticBan($aVals['text'] . ' ' . $aVals['title']);

		// if (!Phpfox::getParam('blog.allow_links_in_blog_title'))
		// {
			// if (!Phpfox::getLib('validator')->check($aVals['title'], array('url')))
			// {
				// return Phpfox_Error::set(Phpfox::getPhrase('blog.we_do_not_allow_links_in_titles'));
			// }
		// }		
		if (!isset($aVals['privacy']))
		{
			$aVals['privacy'] = 0;
		}
		if (!isset($aVals['privacy_comment']))
		{
			$aVals['privacy_comment'] = 0;
		}
		$sTitle = $oFilter->clean($aVals['title'], 255);
		$text = $oFilter->clean($aVals['text'], 255);
		$category_id = $oFilter->clean($aVals['category_id'], 255);
		$cat_parent = $oFilter->clean($aVals['cat_parent'], 255);
		
		// $bHasAttachments = (!empty($aVals['attachment']) && Phpfox::getUserParam('attachment.can_attach_on_blog'));		
		if (!isset($aVals['post_status']))
		{
			$aVals['post_status'] = 1;
		}

		$aInsert = array(
			'user_id' => Phpfox::getUserId(),
			'title' => $sTitle,
			'text' => $text,
			'category_id' => $category_id,
			'time_stamp' => PHPFOX_TIME,
			'privacy' => (isset($aVals['privacy']) ? $aVals['privacy'] : '0'),
			'post_status' => (isset($aVals['post_status']) ? $aVals['post_status'] : '1')			
		);		
		
		$bIsSpam = false;
		/*
		if (Phpfox::getParam('blog.spam_check_blogs'))
		{
			if (Phpfox::getLib('spam')->check(array(
						'action' => 'isSpam',										
						'params' => array(
							'module' => 'blog',
							'content' => $oFilter->prepare($aVals['text'])
						)
					)
				)
			)
			{
				$aInsert['is_approved'] = '9';
				$bIsSpam = true;				
			}
		}
		
		if (Phpfox::getUserParam('blog.approve_blogs'))
		{
			$aInsert['is_approved'] = '0';
			$bIsSpam = true;
		}
		*/
		// (($sPlugin = Phpfox_Plugin::get('blog.service_process_add_start')) ? eval($sPlugin) : false);

		$iId = $this->database()->insert(Phpfox::getT('hoidap_cauhoi'), $aInsert);		
		
		// (($sPlugin = Phpfox_Plugin::get('blog.service_process_add_end')) ? eval($sPlugin) : false);
		
		// $this->database()->insert(Phpfox::getT('blog_text'), array(
				// 'blog_id' => $iId,
				// 'text' => $oFilter->clean($aVals['text']),
				// 'text_parsed' => $oFilter->prepare($aVals['text'])
			// )
		// );
		
		// if (!empty($aVals['selected_categories']))
		// {
			// Phpfox::getService('blog.category')->addCategoryForBlog($iId, explode(',', rtrim($aVals['selected_categories'], ',')), ($aVals['post_status'] == 1 ? true : false));
		// }

		// if (Phpfox::getUserParam('tag.can_add_tags_on_blogs') && Phpfox::isModule('tag') && isset($aVals['tag_list']) && ((is_array($aVals['tag_list']) && count($aVals['tag_list'])) || (!empty($aVals['tag_list']))))
		// {
			// Phpfox::getService('tag.process')->add('blog', $iId, Phpfox::getUserId(), $aVals['tag_list']);
		// }
		
		// If we uploaded any attachments make sure we update the 'item_id'
		// if ($bHasAttachments)
		// {
			// Phpfox::getService('attachment.process')->updateItemId($aVals['attachment'], Phpfox::getUserId(), $iId);
		// }		
	
		// if ($bIsSpam === true)
		// {			
			// return $iId;
		// }		
		
		if ($aVals['post_status'] == 1)
		{
				(Phpfox::isModule('feed') ? Phpfox::getService('feed.process')->add('hoidap', $iId, $aVals['privacy'], null,null,null,null,null,null, $category_id, $cat_parent) : null);
					
			// Update user activity
			// Phpfox::getService('user.activity')->update(Phpfox::getUserId(), 'blog');
		}		
		
		// if ($aVals['privacy'] == '4')
		// {
			// Phpfox::getService('privacy.process')->add('blog', $iId, (isset($aVals['privacy_list']) ? $aVals['privacy_list'] : array()));			
		// }		
		
		// $this->cache()->remove(array('user/' . Phpfox::getUserId(), 'blog_browse'), 'substr');
		
		// (($sPlugin = Phpfox_Plugin::get('blog.service_process__end')) ? eval($sPlugin) : false);	
		
		return $iId;
	}
	
	public function update($aVals, $iId)
	{
		$oFilter = Phpfox::getLib('parse.input');	
		
		if (!isset($aVals['privacy']))
		{
			$aVals['privacy'] = 0;
		}

		$sTitle = $oFilter->clean($aVals['title'], 255);
		$text = $oFilter->clean($aVals['text'], 255);
		$category_id = $oFilter->clean($aVals['category_id'], 255);
		$category_parent = $oFilter->clean($aVals['cat_parent'], 255);
		$aVals['post_status'] = 1;
		
		$this->database()->update(Phpfox::getT('hoidap_cauhoi'), $aInsert);		
		$aUpdate = array(
			// 'user_id' => Phpfox::getUserId(),
			'title' => $sTitle,
			'text' => $text,
			'category_id' => $category_id,
			'time_stamp' => PHPFOX_TIME,
			// 'privacy' => (isset($aVals['privacy']) ? $aVals['privacy'] : '0'),
			'post_status' => (isset($aVals['post_status']) ? $aVals['post_status'] : '1')			
		);	
		
		$aUpdate_feed = array(		
			'category_id' => $category_id,
			'category_parent' => $category_parent				
		);	
		
		$this->database()->update(Phpfox::getT('hoidap_cauhoi'), $aUpdate, 'cauhoi_id = ' . (int) $iId);	
		$this->database()->update(Phpfox::getT('feed'), $aUpdate_feed, 'item_id = ' . (int) $iId);	

		
	}
	
	public function selectCauHoiById($iId)
	{
		$oFilter = Phpfox::getLib('parse.input');	
		$cauhoi_id = $oFilter->clean($iId, 255);
	
		mysql_query("set names'utf8'");
		return Phpfox::getLib('database')->select('hdch.* ')
		->from('phpfox_hoidap_cauhoi hdch')
		->where(' hdch.cauhoi_id ='.$cauhoi_id) 
		->execute('getRow');	
			
	}
}

?>